]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
st/icon-theme: Copy GdkPixbuf utility code
authorFlorian Müllner <fmuellner@gnome.org>
Wed, 1 Feb 2023 14:19:47 +0000 (15:19 +0100)
committerMarge Bot <marge-bot@gnome.org>
Thu, 2 Feb 2023 20:45:06 +0000 (20:45 +0000)
GTK includes a couple of shared private GdkPixbuf utilities
functions. We don't have a need for sharing that code, just
for the bits that are used by the IconTheme code.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2620>

src/st/st-icon-theme.c

index deedc9add9ee98f4afaf40b202f3cfaf748204ac..87cd907ff557067c769a9c4467e508a2f5e06a2c 100644 (file)
@@ -50,7 +50,6 @@
 #include "gtksettingsprivate.h"
 #include "gtkstylecontextprivate.h"
 #include "gtkprivate.h"
-#include "gdkpixbufutilsprivate.h"
 
 #undef GDK_DEPRECATED
 #undef GDK_DEPRECATED_FOR
@@ -3737,6 +3736,118 @@ gtk_icon_info_is_symbolic (GtkIconInfo *icon_info)
   return is_symbolic;
 }
 
+static GdkPixbuf *
+load_from_stream (GdkPixbufLoader  *loader,
+                  GInputStream     *stream,
+                  GCancellable     *cancellable,
+                  GError          **error)
+{
+  GdkPixbuf *pixbuf;
+  gssize n_read;
+  guchar buffer[65536];
+  gboolean res;
+
+  res = TRUE;
+  while (1)
+    {
+      n_read = g_input_stream_read (stream, buffer, sizeof (buffer), cancellable, error);
+      if (n_read < 0)
+        {
+          res = FALSE;
+          error = NULL; /* Ignore further errors */
+          break;
+        }
+
+      if (n_read == 0)
+        break;
+
+      if (!gdk_pixbuf_loader_write (loader, buffer, n_read, error))
+        {
+          res = FALSE;
+          error = NULL;
+          break;
+        }
+    }
+
+  if (!gdk_pixbuf_loader_close (loader, error))
+    {
+      res = FALSE;
+      error = NULL;
+    }
+
+  pixbuf = NULL;
+
+  if (res)
+    {
+      pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
+      if (pixbuf)
+        g_object_ref (pixbuf);
+    }
+
+  return pixbuf;
+}
+
+static void
+size_prepared_cb (GdkPixbufLoader *loader,
+                  int              width,
+                  int              height,
+                  gpointer         data)
+{
+  gdouble *scale = data;
+
+  width = MAX (*scale * width, 1);
+  height = MAX (*scale * height, 1);
+
+  gdk_pixbuf_loader_set_size (loader, width, height);
+}
+
+/* Like gdk_pixbuf_new_from_stream_at_scale, but
+ * load the image at its original size times the
+ * given scale.
+ */
+static GdkPixbuf *
+_gdk_pixbuf_new_from_stream_scaled (GInputStream  *stream,
+                                    gdouble        scale,
+                                    GCancellable  *cancellable,
+                                    GError       **error)
+{
+  GdkPixbufLoader *loader;
+  GdkPixbuf *pixbuf;
+
+  loader = gdk_pixbuf_loader_new ();
+
+  g_signal_connect (loader, "size-prepared",
+                    G_CALLBACK (size_prepared_cb), &scale);
+
+  pixbuf = load_from_stream (loader, stream, cancellable, error);
+
+  g_object_unref (loader);
+
+  return pixbuf;
+}
+
+/* Like gdk_pixbuf_new_from_resource_at_scale, but
+ * load the image at its original size times the
+ * given scale.
+ */
+static GdkPixbuf *
+_gdk_pixbuf_new_from_resource_scaled (const char  *resource_path,
+                                      gdouble      scale,
+                                      GError     **error)
+{
+  GInputStream *stream;
+  GdkPixbuf *pixbuf;
+
+  stream = g_resources_open_stream (resource_path, 0, error);
+  if (stream == NULL)
+    return NULL;
+
+  pixbuf = _gdk_pixbuf_new_from_stream_scaled (stream, scale, NULL, error);
+  g_object_unref (stream);
+
+  return pixbuf;
+}
+
 static GdkPixbuf *
 apply_emblems_to_pixbuf (GdkPixbuf   *pixbuf,
                          GtkIconInfo *info)